package com.example.easyutils.watermark;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.util.DialogLoad;
import com.example.easyutils.util.InputDel;
import com.example.easyutils.util.MyRecycleViewAdapter;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import okhttp3.Call;
import okhttp3.Response;

public class ClearWatermark extends BaseActivity {
    private RecyclerView mRecycleView;
    private MyRecycleViewAdapter myRecycleViewAdapter;//适配器
    private LinearLayoutManager mLinearLayoutManager;//布局管理器
    private List mList;
//    ZLoadingDialog dialog = new ZLoadingDialog(ClearWatermark.this);//加载页面动画
    DialogLoad dialogLoad;
    private EditText editText;
    private ImageView delImageView;
    private Map<String, String> videoUrl;
    private final static int SUCCESS_CODE = 001,ERROR_CODE = 002,WARN_CODE = 003;
    private String vendorUrl,vendorClearWatermark = null,warnStr;
    private TextView urlTip;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            dialogLoad.dialogCancel();
            switch (msg.what)
            {
                case SUCCESS_CODE:
                    String url = null;
                    if(videoUrl.get("url")!="") url = videoUrl.get("url");
//                    Log.d("testst",videoUrl+"");
//                    Log.d("testst22",url+"");
//                    Log.d("111teststs",vendorClearWatermark+"");
                    if(url==null){
                        urlTip.setText("视频路径出错啦");
                        Toast.makeText(ClearWatermark.this,"视频路径出错啦！",Toast.LENGTH_LONG).show();

                    }else{
                        urlTip.setText("解析成功！查看视频");
                        final String finalUrl = url;
                        urlTip.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Uri uri = Uri.parse(finalUrl);
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                startActivity(intent);
                            }
                        });
                    }

//                    "http://baike.baidu.com/view/753813.htm"为你要转到的网站。
                    break;
                case ERROR_CODE:
                    Toast.makeText(ClearWatermark.this,"出错啦！",Toast.LENGTH_LONG).show();
                    break;
                case WARN_CODE:
                    Toast.makeText(ClearWatermark.this,warnStr,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clear_watermark);
        dialogLoad = new DialogLoad(ClearWatermark.this);
        editText = (EditText)findViewById(R.id.editText);
        urlTip = (TextView)findViewById(R.id.url_tip);

        InputDel inputDel = new InputDel(editText,delImageView,ClearWatermark.this);
        inputDel.initListener();
        inputDel.editDel();

        mList = new ArrayList();
        mRecycleView = (RecyclerView)findViewById(R.id.cleaWMRecycleView);
        initData(mList);

        mLinearLayoutManager = new LinearLayoutManager(ClearWatermark.this,LinearLayoutManager.HORIZONTAL,false);
        myRecycleViewAdapter = new MyRecycleViewAdapter(mList,ClearWatermark.this);
        mRecycleView.setLayoutManager(new GridLayoutManager(this, 3));
        mRecycleView.setAdapter(new MyRecycleViewAdapter(mList,ClearWatermark.this));
    }



    public void initData(List list){
        String textList[] = new String[]{"抖音","快手","皮皮虾","微视","哔哩哔哩","好看视频","剪映","微博"};
        int imageList[] = new int[]{R.drawable.douyin,R.drawable.kuaishou,R.drawable.fangpipixia,
        R.drawable.weishi,R.drawable.bilibili,R.drawable.haokan,R.drawable.jianying,R.drawable.weibo};


        for(int i = 0; i < imageList.length; i ++){
            Map map = new HashMap();
            map.put("image",imageList[i]);
            map.put("text",textList[i]);
            list.add(map);
        }
    }

    public void commit(View v){//确定

//        http://v.douyin.com/5w5JwL/
//        https://v.douyin.com/JfSrLuP/
        String url = editText.getText().toString();
        Log.d("url",url);
        if(url.equals("")){
            Toast.makeText(ClearWatermark.this,"视频路径不能为空",Toast.LENGTH_LONG).show();
            return;
        }

        dialogLoad.dialogLoaded("加载中...");//加载动画
        HttpGetRequest.sendOkHttpGetRequst("https://v.u84.net/guest/parse?q="+url,
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("ERRORCODE",e+"");
                        handler.sendEmptyMessage(ERROR_CODE);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        String dataMap = response.body().string();
                        if(dataMap.equals("无效的视频地址") || dataMap.equals("无效的参数") ||
                        dataMap.equals("解析失败: 未能解析PageData")){
                            warnStr = dataMap;
                            handler.sendEmptyMessage(WARN_CODE);
                            return;
                        }

                        Log.d("成功1",dataMap);
                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map>(){

                                }.getType(),new MyTypeAdapter()).create();
                        Map dataDetail = gson.fromJson(dataMap,new TypeToken<Map>(){

                        }.getType());

                        Log.d("SUCCESS1",dataDetail+"");

//                        Log.d("SUCCESS",dataDetail.get("video_url")+"");
//                        videoUrl = dataDetail;
                        handler.sendEmptyMessage(SUCCESS_CODE);

                    }
                });
    }

}
